<?php
// +----------------------------------------------------------------------
// | B5LaravelCMF
// +----------------------------------------------------------------------
// | Author: 李恒 <357145480@qq.com>
// +----------------------------------------------------------------------
namespace App\Services;

use App\Cache\WebPosCache;
use App\Models\WebPos;
use App\Validates\WebPosValidate;

/**
 * 网站广告位置
 * Class WebPosService
 * @package App\Services
 */
class WebPosService extends BaseService
{
    public function __construct(bool $loadValidate = true)
    {
        $this->setModel(new WebPos());
        $loadValidate && $this->setValidate(new WebPosValidate());
    }

    public function getListGroupByWeb(){
        $list=WebPosCache::get();
        $reList=[];
        if($list){
            foreach ($list as $val){
                if($val['website']){
                    $reList[$val['website']][]=$val;
                }
            }
        }
        unset($list);
        return $reList;
    }

    /**
     * 清除缓存
     * @return array
     */
    public function delcache(){
        WebPosCache::clear();
        return message('清理缓存完成', true);
    }

    public function after_add($data)
    {
        WebPosCache::clear();
        return parent::after_add($data); // TODO: Change the autogenerated stub
    }

    public function after_edit($data)
    {
        WebPosCache::clear();
        return parent::after_edit($data); // TODO: Change the autogenerated stub
    }

    public function after_drop($data,$field)
    {
        WebPosCache::clear();
        return parent::after_drop($data,$field); // TODO: Change the autogenerated stub
    }
    /**
     * 删除菜单时，若有子菜单则无法删除
     * @return array
     */
    public function drop()
    {
        $ids = request()->input('ids');
        if ($ids) {
            if (is_array($ids)) {
                $idArr = $ids;
            } else {
                $ids = trim($ids, ',');
                $idArr = explode(',', $ids);
            }
            foreach ($idArr as $id) {
                $id = intval($id);
                if ($id) {
                    $hasChild = (new WebAdService())->info([['pos_id', '=', $id]], true);
                    if ($hasChild) {
                        return message('该位置下含有广告信息，请先删除广告信息', false);
                    }
                }
            }
        }
        return parent::drop(); // TODO: Change the autogenerated stub
    }
}
